package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.zerox.domain.TableVO;
import com.zerox.pojo.Supplier;
import com.zerox.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    
    @GetMapping("/supplierList")
    public String supplierList(){
        return "supplierList";
    }
    
    @GetMapping("/List")
    public void toSupplierList(HttpServletResponse response, Model model, String supContact, Integer page, Integer limit) throws IOException {
        Integer count;
        if (supContact!=null){
            count = supplierService.count(supContact);
        }else{
            count = supplierService.count("");
        }
        List<Supplier> list;
        if (supContact!=null)
            list    = supplierService.getList(supContact,(page-1)*limit,limit);
        else
            list = supplierService.getList("",(page-1)*limit,limit);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        TableVO tableVo = new TableVO(0,count,list,"");
        String json = JSON.toJSONString(tableVo);
        out.print(json);
    }
    
    @GetMapping("/toadd")
    public String toAdd(){
        return "supplierAdd";
    }
    
    @PostMapping("/add")
    public String add(Model model, @Valid Supplier supplier, BindingResult bindingResult, HttpSession session){
        supplier.setCreatedUserId(1);
        supplier.setCreatedTime(new Date());
        int result=supplierService.add(supplier);
        if (result>0){
            return "redirect:supplierList";
        }
        model.addAttribute("errorMsg","新增失败");
        return "supplierAdd";
    }
    
    @GetMapping("/view/{userid}")
    public String view(Model model,@PathVariable("userid") Integer id){
        
        Supplier supplier = supplierService.selectById(id);
        
        model.addAttribute("supplier",supplier);
        
        return "supplierView";
    }
    
    @GetMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Integer id){
        
        int result = supplierService.delete(id);
        return result>0 ? "1" : "0";
    }
    @PostMapping("/update")
    public String update(Model model,Supplier supplier){
        System.out.println(supplier);
         Integer result= supplierService.update(supplier);
         if (result>0)
             return "redirect:supplierList";
        return "redirect:update";
    }
    
    @GetMapping("/update/{id}")
    public String update(Model model, @PathVariable("id")Integer id){
        Supplier  supplier = supplierService.selectById(id);
        model.addAttribute("supplier",supplier);
        return "supplierUpdate";
    }
}
